package com.ningxia.water.mapper;

import com.ningxia.water.entity.TsMeterFlow;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ningxia.water.entity.TsWellWarn;
import com.ningxia.water.vo.out.DataDetailsVo;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author hz
 * @since 2021-03-15
 */
public interface TsMeterFlowMapper extends BaseMapper<TsMeterFlow> {


    /**
     * 根据设备id 查看是否有数据
     *
     * @param meterId
     * @return
     */
    @Select("select count(1) as count from ts_meter_flow mf where mf.meter_id = #{meterId}")
    Integer checkData(Integer meterId);

    /**
     * 查看所有机井的瞬时流量是否符合规定
     *
     * @param timeNow
     * @param timePre
     * @return
     */
    @Select("SELECT\n" +
            "\twm.well_id,\n" +
            "\taw.applicant_id \n" +
            "FROM\n" +
            "\tts_meter_flow mf\n" +
            "\tLEFT JOIN ts_warn_set ws ON ws.data_type = mf.data_type\n" +
            "\tleft join ts_meter m on m.meter_code = mf.meter_id\n" +
            "\tLEFT JOIN ts_well_meter wm ON wm.meter_id = m.meter_id\n" +
            "\tLEFT JOIN ts_applicant_well aw ON aw.well_id = wm.meter_id \n" +
            "WHERE\n" +
            "\tmf.instan_flow < ws.warn_value_min \n" +
            "\tAND mf.read_time BETWEEN #{timeNow} \n" +
            "\tAND #{timePre} UNION\n" +
            "SELECT\n" +
            "\twm.well_id,\n" +
            "\taw.applicant_id \n" +
            "FROM\n" +
            "\tts_meter_flow mf\n" +
            "\tLEFT JOIN ts_warn_set ws ON ws.data_type = mf.data_type\n" +
            "\tleft join ts_meter m on m.meter_code = mf.meter_id\n" +
            "\tLEFT JOIN ts_well_meter wm ON wm.meter_id = m.meter_id\n" +
            "\tLEFT JOIN ts_applicant_well aw ON aw.well_id = wm.meter_id \n" +
            "WHERE\n" +
            "\tmf.instan_flow > ws.warn_value_min \n" +
            "\tAND mf.read_time BETWEEN #{timeNow} \n" +
            "\tAND #{timePre}")
    List<TsWellWarn> monitorVolumeWarnInfos(String timeNow, String timePre);

    /**
     * 获取当天有记录的机井id
     *
     * @return
     */
    @Select("SELECT\n" +
            "\twm.well_id\n" +
            "FROM\n" +
            "\tts_well_meter wm\n" +
            "\tleft join ts_meter m on m.meter_id = wm.meter_id\n" +
            "\tLEFT JOIN ts_meter_flow mf ON mf.meter_id = m.meter_code\n" +
            "WHERE\n" +
            "\tmf.read_time BETWEEN #{startTime} \n" +
            "\tAND #{endTime} \n" +
            "GROUP BY\n" +
            "\twm.well_id")
    List<Integer> getWellIdOfRecords(String startTime, String endTime);

    /**
     * 获取所有的机井id
     * @return
     */
    @Select("SELECT well_id from ts_well_meter ")
    List<Integer> getAllWellIds();

    /**
     * 检测电池电量不足的机井设备
     * @param startTime
     * @param endTime
     * @return
     */
    @Select("SELECT\n" +
            "\twm.meter_id,\n" +
            "\taw.applicant_id \n" +
            "FROM\n" +
            "\tts_meter_flow mf\n" +
            "\tLEFT JOIN ts_meter m ON mf.meter_id = m.meter_code\n" +
            "\tLEFT JOIN ts_well_meter wm ON wm.meter_id = m.meter_id\n" +
            "\tLEFT JOIN ts_applicant_well aw ON aw.well_id = wm.meter_id \n" +
            "WHERE\n" +
            "\tmf.read_time BETWEEN #{startTime} \n" +
            "\tAND #{endTime} \n" +
            "\tAND mf.battery < '1' \n" +
            "GROUP BY\n" +
            "\twm.meter_id,\n" +
            "\taw.applicant_id")
    List<TsWellWarn> monitorMeterInfos(String startTime, String endTime);


    @Select("<script>" +
            "SELECT " +
            "mf.id," +
            "mf.meter_id," +
            "mf.instan_flow / 10000 as instanFlow," +
            "mf.accrued_flow / 10000 as accruedFlow," +
            "mf.battery, " +
            "mf.read_time, " +
            "mf.pressure, " +
            "mf.create_time, " +
            "mf.data_type, " +
            "m.meter_name, " +
            "wi.well_name, " +
            "ai.applicant_name\n" +
            "FROM ts_meter_flow mf \n" +
            "LEFT JOIN ts_meter m ON mf.meter_id = m.meter_code\n" +
            "LEFT JOIN ts_well_meter wm ON m.meter_id = wm.meter_id\n" +
            "LEFT JOIN ts_well_info wi ON wm.well_id = wi.well_id\n" +
            "LEFT JOIN ts_applicant_well aw ON wi.well_id = aw.well_id\n" +
            "LEFT JOIN ts_applicant_info ai ON aw.applicant_id = ai.applicant_id\n" +
            "<where>" +
            "<if test='meterId!=null'> " +
            "and mf.meter_id = #{meterId} " +
            "</if> " +
            "<if test='wellName!=null'> " +
            "and wi.well_name like concat('%', #{wellName}, '%') " +
            "</if> " +
            "<if test='applicantName!=null'> " +
            "and ai.applicant_name like concat('%', #{applicantName}, '%') " +
            "</if> " +
            "</where>" +
            "</script>")
    List<DataDetailsVo> getDataDetails(String meterId, String wellName, String applicantName);
}
